/*
 *    Copyright (c) 2018-2025, 云集汇通 All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yunjihuitong.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 */

package com.yjht.modules.weixin.controller;

import com.yjht.common.func.validator.ValidatorUtils;
import com.yjht.common.utils.PageUtils;
import com.yjht.common.utils.Query;
import com.yjht.common.utils.R;
import com.yjht.modules.sys.entity.SysDictEntity;
import com.yjht.modules.sys.service.SysDictService;
import com.yjht.modules.weixin.entity.WxWechatRelationAreaEntity;
import com.yjht.modules.weixin.entity.WxcmsAccountEntity;
import com.yjht.modules.weixin.service.WxWechatRelationAreaService;
import com.yjht.modules.weixin.service.WxcmsAccountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 微信公众号列表
 *
 * @author lengleng
 * @email wangiegie@gmail.com
 * @date 2018-06-14 10:47:40
 */
@RestController
@RequestMapping("wxcms/account")
public class WxcmsAccountController {
    @Autowired
    private WxcmsAccountService wxcmsAccountService;
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private WxWechatRelationAreaService wxWechatRelationAreaService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:wxcmsaccount:list")
    public R list(@RequestParam Map<String, Object> params){
        //PageUtils page = wxcmsAccountService.queryPage(params);
        Query<WxcmsAccountEntity> query = new Query<>(params);
        PageUtils page = wxcmsAccountService.queryWxAccountPage(params,query);
        //主题信息
        String type = "themes";
        SysDictEntity dict = new SysDictEntity();
        dict.setType(type);
        dict.setParentid(0L);
        List<SysDictEntity> list = sysDictService.queryList(dict);
        return R.ok().put("page",page).put("theme",list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:wxcmsaccount:info")
    public R info(@PathVariable("id") Integer id){
        WxcmsAccountEntity wxcmsAccount = wxcmsAccountService.selectById(id);

        return R.ok().put("wxcmsAccount", wxcmsAccount);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:wxcmsaccount:save")
    public R save(@RequestBody WxcmsAccountEntity wxcmsAccount){
        wxcmsAccountService.insert(wxcmsAccount);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("sys:wxcmsaccount:update")
    @Transactional
    public R update(){//@RequestBody WxcmsAccountEntity wxcmsAccount
        WxcmsAccountEntity wxcmsAccount = new WxcmsAccountEntity();
        wxcmsAccount.setId(3);
        wxcmsAccount.setAppid("appid");
        wxcmsAccount.setAccount("test1");
        wxcmsAccount.setAreaIds(new int[]{10,9});
        ValidatorUtils.validateEntity(wxcmsAccount);
        //公众号信息更新
        boolean flagWxAccount = wxcmsAccountService.updateById(wxcmsAccount);
        //更新关联区域
        int[] areaIds = wxcmsAccount.getAreaIds();
        int wxid = wxcmsAccount.getId();
        boolean flag = false;
        if(areaIds != null && areaIds.length > 0){
            flag = wxWechatRelationAreaService.updateOrSaveArea(wxid,areaIds);
        }
        if(flag == false){
            return R.error();
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:wxcmsaccount:delete")
    public R delete(@RequestBody Integer[] ids){
        wxcmsAccountService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 企业号信息
     */
    @RequestMapping("/qyInfo/{id}")
    public R qyInfo(@PathVariable("id") Integer id){
        WxcmsAccountEntity wxcmsAccount = wxcmsAccountService.selectById(id);
        return R.ok().put("qyInfo", wxcmsAccount);
    }
    /**
     * 企业号关联区域信息
     */
    @RequestMapping("/relationArea/{wxid}")
    public R wechatRelationArea(@PathVariable("wxid") Integer wxid){
        List<WxWechatRelationAreaEntity> list = wxWechatRelationAreaService.queryRelationArea(wxid);
        return R.ok().put("relationArea", list);
    }
}
